package com.efast.dmom.mes.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.portal.bean.common.PortalUserSecurityBean;

public class SpringWebUtils {

	// 缓存类型 本地session
	public static String CACHE_TYPE_SESSION = "LOCAL_SESSION";

	public static String CHAHE_TYPE_REQUEST = "LOCAL_REQUEST";

	public static HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
	}

	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static PortalUserSecurityBean getRequestUserObj(){
		Object user = getSession().getAttribute(CommonConstant.PORTAL_USER_OBJECT);
		if (null == user) {
			return null;
		}
		return (PortalUserSecurityBean)user;
	}

	public static String getRequestUser() {
		Object user = getSession().getAttribute(CommonConstant.CURRENT_LOGIN_USER_NAME_KEY);
	
		return null != user ? user.toString() : "";
	}

	public static String getRequestCompany() {
		Object company = getSession().getAttribute(CommonConstant.CURRENT_LOGIN_USER_COMPANYCODE_KEY);
		return null != company ? company.toString() : "";
	}

	public static void putAttribute(String key, Object value, String cacheType) {
		if (cacheType.equals(CACHE_TYPE_SESSION))
			getSession().setAttribute(key, value);
		else if (cacheType.equals(CHAHE_TYPE_REQUEST))
			getRequest().setAttribute(key, value);
	}

	public static Object getAttribute(String key, String cacheType) {
		if (cacheType.equals(CACHE_TYPE_SESSION))
			return getSession().getAttribute(key);
		else if (cacheType.equals(CHAHE_TYPE_REQUEST))
			return getRequest().getAttribute(key);

		return null;
	}
}
